/**
 * 地区解析
 * 2015-11-03
 */
(function(undefined){
    var district_obj = {};
	district_obj.ajax_url='';

    //初始化
    district_obj.ini = function(param,dis){
		
		$.get(district_obj.ajax_url,{'pid': 0},
			function(result){    
                $('.'+param+':eq(0)').show();			
				if(!result){return false;}
				$.each(result, function(i, field){
					if(!field){return true;}
					if(1==dis){
					var html = '<optgroup label="'+i.toUpperCase()+'">';
					$.each(field,function(j,val){
						html += "<option value=" + val['id'] + ">" + val['name'] + "</option>" ;
					})
					html += '</optgroup>';
					}else{
						html += "<option value=" + field['id'] + ">" + field['name'] + "</option>" ;
					}
					$('.'+param+':eq(0)').append(html);  
				});				
		});		
		
        //绑定选择事件
        $('.' + param).bind({
            change: function(){
                var value = this.value;                
				var next = $(this).next('.' + param);
                $(this).nextAll('.' + param).hide();                				
                next.empty();				
                next.append("<option value='0'>--请选择--</option>");	                				
                $.get(district_obj.ajax_url,{'pid': value},
                    function(result){                                           
                        if(!result){return false;}
                        $.each(result, function(i, field){
                            if(!field){return true;}
					if(1==dis){
					var html = '<optgroup label="'+i.toUpperCase()+'">';
					$.each(field,function(j,val){
						html += "<option value=" + val['id'] + ">" + val['name'] + "</option>" ;
					})
					html += '</optgroup>';
					}else{
						html += "<option value=" + field['id'] + ">" + field['name'] + "</option>" ;
					}
                            next.append(html);  
                        });
						next.show();						
                    });
            }
        })
		
    }


    //将插件类赋给window , 然后就能直接调用
    if (window.district_obj == undefined) {
        window.district_obj = district_obj;
    }
})();
